package com.lzy.business.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.datasource.multiple.DataSourceHolder;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.member.apis.MemberSaleLogServiceApi;
import com.lzy.member.dtos.MemberSaleOrderDto;
import com.lzy.order.dtos.OrderInfoDTO;
import com.lzy.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by WIN7 on 2017/12/05.
 */
@Component
public class OrderFacade {

    @Autowired
    private MemberSaleLogServiceApi memberSaleLogServiceApi;

    @Autowired
    private OrderService orderService;

    public PageInfo<MemberSaleOrderDto> motorOrderList(Pager pager, MemberSaleOrderDto memberSaleOrderDto) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        PageInfo<MemberSaleOrderDto> pageInfo = memberSaleLogServiceApi.selectMotorOrderList(pager, memberSaleOrderDto);
        List<MemberSaleOrderDto> list = pageInfo.getList();
        if (list != null && list.size() > 0) {
            for (MemberSaleOrderDto saleOrderDto : list) {
                OrderInfoDTO orderInfoDTO = orderService.selectOrderInfoByOrderId(Long.valueOf(saleOrderDto.getOrderId()));
                saleOrderDto.setOrderAmount(orderInfoDTO.getItemAmount());
                if (saleOrderDto != null && saleOrderDto.getCount() != null && saleOrderDto.getOrderAmount() != null) {
                    saleOrderDto.setPrice(saleOrderDto.getOrderAmount().divide(new BigDecimal(saleOrderDto.getCount())));
                }
                saleOrderDto.setName(saleOrderDto.getName() != null ? saleOrderDto.getName() : "");
                saleOrderDto.setAccount(saleOrderDto.getAccount() != null ? saleOrderDto.getAccount() : "");
            }
        }
        DataSourceHolder.clearDataSource();
        pageInfo.setList(list);
        return pageInfo;
    }

    public MemberSaleOrderDto dataStatistics(MemberSaleOrderDto memberSaleOrderDto) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        MemberSaleOrderDto saleOrder = memberSaleLogServiceApi.selectAmountMotor(memberSaleOrderDto);
        DataSourceHolder.clearDataSource();
        return saleOrder;
    }
}